/*
Implement getpwnam() using setpwent(), getpwent(), and endpwent().
 */

#include <stdio.h>
#include <stdlib.h>
#include <pwd.h>
#include <string.h>
#include <assert.h>


struct passwd*
getpwnam(const char *name)
{
  puts("inside getpwnam\n");
  struct passwd *entry = NULL;
  while ((entry = getpwent()) != NULL)
    {
      if (strcmp(entry->pw_name, name) == 0)
        {
          endpwent();
          return entry;
        }
    }

  return NULL;
}

void
print_passwd(const struct passwd *passwd)
{
  printf("%s:%s:%ld:%ld:%s:%s:%s\n",
         passwd->pw_name,
         passwd->pw_passwd,
         passwd->pw_uid,
         passwd->pw_gid,
         passwd->pw_gecos,
         passwd->pw_dir,
         passwd->pw_shell);
}

/*
daniel:x:1000:1000:daniel:/home/daniel:/usr/bin/zsh
*/

void
tst_passwd_daniel(void)
{
  print_passwd(getpwnam("daniel"));
  assert(NULL == getpwnam("ass"));
}

int
main(void)
{
  tst_passwd_daniel();
  return EXIT_SUCCESS;
}
